package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.InviteBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.InviteAnswerEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/25.
 */

public class InviteAnswerLogic {
    private final Context context;
    private final ArrayList<InviteBean> dataList;
    private LogicCallBack callBack;
    private int pageIndex;

    public InviteAnswerLogic(Activity activity, ArrayList<InviteBean> dataList, LogicCallBack callBack) {
        this.context = activity;
        this.dataList = dataList;
        this.callBack = callBack;
    }

    public void refresh() {
        pageIndex = 1;
        load(pageIndex);
    }

    public void load() {
        pageIndex = pageIndex + 1;
        load(pageIndex);
    }

    private void load(final int pageIndex) {
        JSONObject json = new JSONObject();
        json.put("pageIndex", pageIndex);
        httpCallForm(BBSConfig.listInviterecordByBepin, json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onNoResponse(int code) {
                        if (code==204){
                            dataList.clear();
                        }
                        callBack.onResult(false);
                    }
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        TypeReference<PageEntity<InviteAnswerEntity>> reference = new TypeReference<PageEntity<InviteAnswerEntity>>() {
                        };
                        PageEntity<InviteAnswerEntity> response = parse.parsePageEntity(reference);
                        List<InviteAnswerEntity> data = response.getData();
                        ArrayList<InviteBean> list = new ArrayList<>();
                        for (InviteAnswerEntity entity : data) {
                            InviteBean bean = new InviteBean();

                            bean.setId(entity.getId());

                            TopicBean topic = new TopicBean();
                            topic.setTopicTitle(entity.getTopicTitle());
                            topic.setTopicCode(entity.getTopicCode());
                            bean.setTopic(topic);

                            BBSUserBean user = new BBSUserBean();
                            user.setUserPin(entity.getPin());
                            user.setUserName(entity.getName());
                            bean.setInviteUser(user);
                            bean.setObjectId(entity.getObjectId());
                            list.add(bean);
                        }
                        if (pageIndex == 1) {
                            dataList.clear();
                        }
                        dataList.addAll(list);
                        boolean nextPageAvailable = response.getNextPageAvailable();
                        callBack.onResult(nextPageAvailable);
                    }
                }));
    }


    public interface LogicCallBack {
        void onResult(boolean hasMore);
    }
}
